#include "examplewindow.h"
#include "ui_examplewindow.h"
#include <QMessageBox>

ExampleWindow::ExampleWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ExampleWindow)
    ,mShareMem(nullptr)
{
    ui->setupUi(this);
    on_comboBox_model_currentIndexChanged(0);
    on_checkBox_canWrite_stateChanged(0);
}

ExampleWindow::~ExampleWindow()
{
    delete ui;
}

//模式切换-设置是否可写
void ExampleWindow::on_comboBox_model_currentIndexChanged(int index)
{
    SynShareMem::Type type = (SynShareMem::Type)(index + 1);
    bool isWrite = type < SynShareMem::eRead_1_sema;    //写入模式
    ui->checkBox_canWrite->setChecked(isWrite);
    ui->checkBox_canWrite->setEnabled(!isWrite);
    ui->lineEdit_semaKey->setEnabled(type != SynShareMem::eWrite_1_sema);   //直接写入模式不需要信号量
}

//是否可写状态改动-设置写入按钮是否显示
void ExampleWindow::on_checkBox_canWrite_stateChanged(int arg1)
{
    Q_UNUSED(arg1);
    ui->pushButton_write->setHidden(!ui->checkBox_canWrite->isChecked());
    ui->pushButton_cleanMem->setHidden(!ui->checkBox_canWrite->isChecked());
}

//设置
void ExampleWindow::on_pushButton_setting_clicked()
{
    //获取共享内存参数
    SynShareMem::Type type = (SynShareMem::Type)(ui->comboBox_model->currentIndex() + 1);
    QString memKey = ui->lineEdit_memKey->text();
    int memSize = ui->lineEdit_size->text().toUInt();
    QString semaKey = ui->lineEdit_semaKey->text();
    //bool canWrite = ui->checkBox_canWrite->isChecked();

    if(mShareMem)   //已经存在
    {
        mShareMem->stop();
        while (mShareMem->isRunning());
        delete mShareMem;
        mShareMem = nullptr;
    }
    mShareMem = new SynShareMem(type,memKey,memSize,semaKey,this);

    //读取模式需要绑定信号接收数据并显示
    if(type == SynShareMem::eRead_1_sema || type == SynShareMem::eRead_2_sema)
    {
        connect(mShareMem,&SynShareMem::haveRead,this,[=](char *addr,int size){
            Q_UNUSED(size)
            QString memStr = QString(QLatin1String(addr));
            ui->textEdit->setText(memStr);
        });
    }

    mShareMem->start();
    QMessageBox::information(this,"提示","设置成功！");
}

//手动写入共享内存
void ExampleWindow::on_pushButton_write_clicked()
{
    if(mShareMem == nullptr)
    {
        QMessageBox::information(this,"提示","当前不可写入，请先进行设置！");
        return;
    }
    //记录按键文本，并改变按钮为写入中状态
    QString buffStr = ui->pushButton_write->text();
    ui->pushButton_write->setText("写入中...");
    ui->pushButton_write->setEnabled(0);
    //读取文本编辑框并写入共享内存
    QByteArray textData = ui->textEdit->toPlainText().toLatin1(); // must
    char *source = textData.data();
    bool result = mShareMem->writeMem(source,textData.size());
    //弹窗提示写入结果
    if(result)
        QMessageBox::information(this,"提示","写入成功");
    else
        QMessageBox::information(this,"提示","写入失败");
    //恢复按钮
    ui->pushButton_write->setText(buffStr);
    ui->pushButton_write->setEnabled(1);
}

//手动读取共享内存（读取模式下会自动同步读取，包括内存被清空）
void ExampleWindow::on_pushButton_read_clicked()
{
    if(mShareMem == nullptr)
    {
        QMessageBox::information(this,"提示","当前不可读取，请先进行设置！");
        return;
    }
    QByteArray buff(ui->lineEdit_size->text().toInt(),0);
    int size = mShareMem->readMem(buff.data(),buff.size());
    QString memStr = QString(buff.mid(0,size));
    ui->textEdit->setText(memStr);
}

//清空共享内存
void ExampleWindow::on_pushButton_cleanMem_clicked()
{
    if(mShareMem == nullptr)
    {
        QMessageBox::information(this,"提示","当前不可清空，请先进行设置！");
        return;
    }
    mShareMem->cleanMem(0x00);
    QMessageBox::information(this,"提示","共享内存已清空");
}
//清空编辑框
void ExampleWindow::on_pushButton_cleanText_clicked()
{
    ui->textEdit->clear();
}
